package com.umakr.ax.common.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Maps;
import com.umakr.ax.common.enm.AttentionFrom;
import com.umakr.ax.common.mapper.FavoriteMapper;
import com.umakr.ax.common.model.Favorite;
import com.umakr.ax.common.model.RadioStationShow;
import com.umakr.ax.common.vo.FavoriteListVO;
import com.umakr.ax.core.base.BaseService;
import com.umakr.ax.utils.FormatPhotoUrlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * ${table.comment} 服务实现类
 * </p>
 *
 * @author Umakr-Code-Generator
 * @since 2017-12-30
 */
@Service
public class FavoriteService extends BaseService<FavoriteMapper, Favorite>{
    @Autowired
    RadioStationFollowerService radioStationFollowerService;
    @Autowired
    RadioStationShowService radioStationShowService;
    @Autowired
    Environment env;

    public Object favorite(Long userId, Long rsId, String rsName, String rsLogo, Long universalId, String turnToUrlName, Integer favType,String favTitle, Integer type) {
        String aliUrl = env.getProperty("oss.img.path");
        int errorCode = 0 ;
        String msg = "操作失败";
        Date date = new Date();
        Map<String,Object> data = Maps.newLinkedHashMap();
        //type == 1 表示收藏 type == 0 表示取消收藏
        if(type >= 0 && type  <= 1){
            //根据用户ID和电台秀ID 查找用户是收藏过
            EntityWrapper<Favorite> entityWrapper = new EntityWrapper<>();
            entityWrapper.eq("user_id",userId);
            entityWrapper.eq("universal_id",universalId);
            Favorite favorite = selectOne(entityWrapper);
            //如果是点赞 新增记录 否则删除记录
            if(favorite == null){
                if(type == 0){
                    msg = "取消成功";
                    errorCode = 1;
                }else {
                    favorite = new Favorite();
                    favorite.setUserId(userId);
                    favorite.setRadioId(rsId);
                    favorite.setRsName(rsName);
                    favorite.setRsAvatar(rsLogo==null?null:rsLogo.replace(aliUrl,""));
                    favorite.setUniversalId(universalId);
                    favorite.setTurnPageName(turnToUrlName);
                    favorite.setFavType(favType);
                    favorite.setFavTitile(favTitle);
                    favorite.setFavTime(date);
                    favorite.setDelStaus(0);
                    Favorite record = update(favorite);
                    data.put("id",record.getId());
                    data.put("userId",record.getUserId());
                    data.put("rsId",record.getRadioId());
                    data.put("rsName",record.getRsName());
                    data.put("rsLogo",FormatPhotoUrlUtil.formatString(aliUrl,favorite.getRsAvatar()));
                    data.put("favTime",record.getFavTime());
                    data.put("favType",record.getFavType());
                    data.put("favTitle",record.getFavTitile());
                    data.put("universalId",record.getUniversalId());
                    data.put("turnToUrlName",record.getTurnPageName());
                    radioStationFollowerService.autoAttention(userId,rsId, AttentionFrom.FAVORITE);
                    msg = "收藏成功";
                    errorCode = 1;
                }
            }else if (favorite != null ){
                data.put("id",favorite.getId());
                data.put("userId",favorite.getUserId());
                data.put("rsId",favorite.getRadioId());
                data.put("rsName",favorite.getRsName());
                data.put("rsLogo",FormatPhotoUrlUtil.formatString(aliUrl,favorite.getRsAvatar()));
                data.put("favTime",favorite.getFavTime());
                data.put("favType",favorite.getFavType());
                data.put("favTitle",favorite.getFavTitile());
                data.put("universalId",favorite.getUniversalId());
                data.put("turnToUrlName",favorite.getTurnPageName());
                if(type == 1){
                    radioStationFollowerService.autoAttention(userId,rsId, AttentionFrom.FAVORITE);
                    favorite.setDelStaus(0);
                    msg = "收藏成功";
                }else {
                    favorite.setDelStaus(1);
                    msg = "取消成功";
                }
                favorite.setFavTime(date);
                update(favorite);

                errorCode = 1;
            }else {
                msg = "操作失败";
            }
        }
        return  resultMap(data,errorCode,msg);
    }
    public Object listMyFavoriteByUserId(Long userId ){
        String aliUrl = env.getProperty("oss.img.path");
        Page<FavoriteListVO> favPage = baseMapper.listMyFavoriteByUserId(userId);

        for(FavoriteListVO vo : favPage.getRecords()){
            switch (vo.getFavType()){
                case 0:
                    vo.setTypeName("电台秀");
                    break;
                case 1:
                    vo.setTypeName("活动");
                    break;
                    default:
            }
            if(vo.getFavType() == 0){
                EntityWrapper<RadioStationShow> radioStationShowWrapper = new EntityWrapper<>();
                radioStationShowWrapper.eq("id",vo.getUniversalId());
                RadioStationShow radioStationShow =radioStationShowService.selectOne(radioStationShowWrapper);
                if(radioStationShow != null ){
                    vo.setRadioStationShowDel(radioStationShow.getDelStatus());
                }
            }else if(vo.getFavType() == 1){
                //收藏活动


            }



            vo.setRsLogo(FormatPhotoUrlUtil.formatString(aliUrl,vo.getRsLogo()));
        }

        return favPage;
    }


    public Map<String,Object> deleteMyFavorite(Long fvId ){
        int code = 0;
        String msg = "";

        boolean isdelete = deleteById(fvId);

        if(isdelete){
            code = 1;
            msg = "删除成功";
        }else{
            msg = "删除失败";
        }

        return resultMap(code,msg);
    }

}
